//
//  MyNotesViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-11-21.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "MyNotesViewController.h"
#import "ManagerMyNotesViewController.h"
#import "MyNotesTableViewCell.h"
#import "MyCollectNotesResult.h"
#import "MyCollectNotesStatus.h"
 

@interface MyNotesViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView * myNotesTab;

@property (nonatomic, strong)NSMutableArray * myNotesArr;
@property (nonatomic, assign)NSInteger pageNum;
@end

@implementation MyNotesViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.pageNum = 1;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self initNotesData:self.pageNum];
 }
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
 }
-(void)initNotesData :(NSInteger)page
{
    [LSZRequestResolutionDic initWithMyCollection:[NSString stringWithFormat:@"%ld",(long)page]  type:@"notesAdd" :^(BOOL isOk, NSString * str, LSZMyCollectionManager * manager) {
        if (isOk == YES) {
            if (page == 1) {
                [self.notesArrayData removeAllObjects];
                [self.notesArrayData setArray:manager.mutDataArray];
                if(manager.mutDataArray.count == 0){
                    self.myNotesTab.mj_footer.hidden = YES;
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:self.view];
                }else{
                    [self.myNotesTab reloadData];
                }
            }
            else{
                if(manager.mutDataArray.count == 0){
                    [self.myNotesTab.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.notesArrayData addObjectsFromArray:manager.mutDataArray];
                }
                [self.myNotesTab reloadData];
            }
        }else{
            [self addHubPromptMessage:str];
        }
    }];
}
-(NSMutableArray *)notesArrayData
{
    if (_notesArrayData == nil) {
        _notesArrayData = [[NSMutableArray alloc]init];
    }
    return _notesArrayData;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏字体颜色
    self.navigationItem.title = @"我的笔记";
    [self setupUI];
}
- (void)setupUI
{
    self.myNotesTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
    self.myNotesTab.dataSource = self;
    self.myNotesTab.delegate = self;
    //添加以下代码关闭估算行高
    self.myNotesTab.estimatedRowHeight = 0;
    self.myNotesTab.estimatedSectionHeaderHeight = 0;
    self.myNotesTab.estimatedSectionFooterHeight = 0;
    self.myNotesTab.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.myNotesTab.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.myNotesTab.separatorColor = divider;
    [self.view addSubview:_myNotesTab];
    [self.myNotesTab registerClass:[MyNotesTableViewCell class] forCellReuseIdentifier:@"MyNotesTableViewCell"];
    //收藏的上下拉
    self.myNotesTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageNum = 1;
        [self initNotesData:self.pageNum];
        [self.myNotesTab.mj_header endRefreshing];
    }];
    // 上拉刷新
    self.myNotesTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageNum++;
        [self initNotesData:self.pageNum];
        [self.myNotesTab.mj_footer endRefreshing];
    }];
    //cell分割线顶头
    if ([self.myNotesTab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.myNotesTab setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self.myNotesTab respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.myNotesTab setLayoutMargins: UIEdgeInsetsZero];
    }
}
#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.notesArrayData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.notesArrayData[indexPath.row];
    CGFloat a ;
    if (ScreenWidth == 320) {
        a = 13;
    }else if(ScreenWidth == 375){
        a = 13.5;
    }else {
        a = 14;
    }
    CGFloat height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-20 height:ScreenHeight*4 font:a :collectionMg.content];
    if (ScreenWidth == 320) {
        return 75-16+height;
    }else if(ScreenWidth == 375){
        return 81-18+height;
    }else {
        return 87-20+height;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyNotesTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyNotesTableViewCell" forIndexPath:indexPath];
    cell.createStr = @"";
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.notesArrayData[indexPath.row];

    cell.titleLabel.text = [NSString stringWithFormat:@"%@",collectionMg.title];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",collectionMg.addtime];
    cell.contentLabel.text = [NSString stringWithFormat:@"%@",collectionMg.content];
    cell.aboutLabel.text = [NSString stringWithFormat:@"相关经文:%@",collectionMg.title];
    CGFloat a ;
    if (ScreenWidth == 320) {
        a = 13;
    }else if(ScreenWidth == 375){
        a = 13.5;
    }else {
        a = 14;
    }
    CGFloat height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-20 height:ScreenHeight*4 font:a :collectionMg.content];

    CGRect rect = cell.contentLabel.frame;
    rect.size.height = height;
    cell.contentLabel.frame = rect;

    CGRect rect1 = cell.aboutLabel.frame;
    rect1.origin.y = CGRectGetMaxY(cell.contentLabel.frame)+4;
    cell.aboutLabel.frame = rect1;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LSZCollectionMg * collectionMg = nil;
    collectionMg = self.notesArrayData[indexPath.row];

    ManagerMyNotesViewController * managerNotesVC = [[ManagerMyNotesViewController alloc] init];
    managerNotesVC.partitionStr = @"1";
    managerNotesVC.myNotesId = [NSString stringWithFormat:@"%@",collectionMg.id_id];
    managerNotesVC.shengJingTitle = [NSString stringWithFormat:@"%@",collectionMg.title];
    managerNotesVC.shengJingMessageStr = [NSString stringWithFormat:@"%@",collectionMg.input];
    managerNotesVC.notesContentStr = [NSString stringWithFormat:@"%@",collectionMg.content];
    managerNotesVC.myNotesVC = self;
    [self.navigationController pushViewController:managerNotesVC animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        LSZCollectionMg * collectionMg = nil;
        collectionMg = self.notesArrayData[indexPath.row];
        [self sendDeleteNote: collectionMg.id_id :indexPath.row];
    }
}
-(void)sendDeleteNote :(NSString *)resource_id :(int)row
{
    [LSZRequestData initDeleteNotes:resource_id :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if (isOk == YES) {
            [self initWithtring:string];
            [self.notesArrayData removeObjectAtIndex:row];
            [self.myNotesTab reloadData];
        }else{
            [self initWithtring:string];
        }
    }];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark - 懒加载
- (NSMutableArray *)myNotesArr
{
    if (_myNotesArr == nil) {
        self.myNotesArr = [NSMutableArray array];
    }
    return _myNotesArr;
}
@end
